package com.jian.java.config;

import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author Jiange
 * @create 2021-05-07-14:19
 */
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode("1");
        auth.inMemoryAuthentication()
                .withUser("lizijian").password(encode).roles("vip1","vip2")
                .and()
                .withUser("root").password(encode).roles("vip1","vip2","vip3")
        ;
//        auth.userDetailsService().passwordEncoder(bCryptPasswordEncoder);
    }

    @Bean
    PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/").permitAll()
                .antMatchers("/vip1").hasRole("vip1")
                .antMatchers("/vip2").hasRole("vip2")
                .antMatchers("/vip3").hasRole("vip3");

        http.formLogin();
        http.logout().deleteCookies("remove").invalidateHttpSession(true)
                    .logoutUrl("/")
                    .logoutSuccessUrl("/")
        ;
        http.rememberMe();
    }
}
